package com.interrobang.gui;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.interrobang.anime.Anime;
import com.interrobang.anime.AnimeList;

public class ViewAnimePanel extends JPanel {
	private static final long serialVersionUID = -5004823720293347103L;
	private MainPanel panel;
	AnimeList list;
	private final String FILENAME = "Data/data.haruhi";
	JList jList;
	JScrollPane listPane;
	JButton back;
	ButtonListener listener = new ButtonListener();
	JButton select;
	JButton remove;
	private JLabel title;

	public ViewAnimePanel(MainPanel panel) {
		this.panel = panel;
		listener = new ButtonListener();
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try {
			fis = new FileInputStream(FILENAME);
			in = new ObjectInputStream(fis);
			list = (AnimeList) in.readObject();
			in.close();
		} catch (IOException e) {
			list = new AnimeList();
		} catch (ClassNotFoundException e) {
			// do something here
		}
		initComponents();
		update();
	}
	

	private void initComponents() {

		listPane = new javax.swing.JScrollPane();

		select = new javax.swing.JButton();
		remove = new javax.swing.JButton();
		back = new javax.swing.JButton();
		title = new javax.swing.JLabel();

		select.addActionListener(listener);
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try {
			fis = new FileInputStream(FILENAME);
			in = new ObjectInputStream(fis);
			list = (AnimeList) in.readObject();
			in.close();
		} catch (IOException e) {
			list = new AnimeList();
		} catch (ClassNotFoundException e) {
			// do something here
		}

		String[] entries = new String[list.size()];
		for (int i = 0; i < entries.length; i++) {
			entries[i] = list.get(i).getTitle();
		}
		jList = new javax.swing.JList(entries);
		jList.addListSelectionListener(listener);
		listPane.setViewportView(jList);

		select.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
		select.setText("Select");

		remove.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
		remove.setText("Remove");

		back.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
		back.setText("Back to Main Menu");
		back.addActionListener(listener);
		remove.addActionListener(listener);

		title.setFont(new java.awt.Font("Comic Sans MS", 0, 24)); // NOI18N
		title.setText("Select an Anime");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(122,
																		122,
																		122)
																.addComponent(
																		title))
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
																.addGroup(
																		javax.swing.GroupLayout.Alignment.LEADING,
																		layout.createSequentialGroup()
																				.addGap(81,
																						81,
																						81)
																				.addComponent(
																						listPane,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						270,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGroup(
																		layout.createSequentialGroup()
																				.addContainerGap()
																				.addGroup(
																						layout.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																								.addComponent(
																										back,
																										javax.swing.GroupLayout.Alignment.TRAILING,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE)
																								.addGroup(
																										javax.swing.GroupLayout.Alignment.TRAILING,
																										layout.createSequentialGroup()
																												.addComponent(
																														select)
																												.addPreferredGap(
																														javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																												.addComponent(
																														remove))))))
								.addContainerGap(82, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(title)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(listPane,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										82,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(remove)
												.addComponent(select))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(back)
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
	}

	public void update() {
		removeAll();
		initComponents();
		updateUI();
	}

	private class ButtonListener implements ActionListener,
			ListSelectionListener {
		private int index = 0;

		@Override
		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == back) {
				FileOutputStream fos = null;
				ObjectOutputStream out = null;
				try {
					fos = new FileOutputStream(FILENAME);
					out = new ObjectOutputStream(fos);
					out.writeObject(list);
					out.close();
				} catch (IOException ex) {
					System.out.print("File not found");
				}
				panel.setAddActive(false);
				panel.setMenuActive(true);
				panel.setViewActive(false);
				panel.update();
			}
			if (event.getSource() == select) {
				if (list.size() == 0) {
					JOptionPane.showMessageDialog(panel,
							"There are currently no anime's to select");
				} else {
					Anime anime = list.get(index);
					setVisible(false);
					AnimePanel Apanel = new AnimePanel(anime, panel, list);
					panel.setViewActive(false);
					panel.update();

					panel.add(Apanel);
					Apanel.update();
				}
			}
			if (event.getSource() == remove) {
				if (list.size() == 0) {
					JOptionPane.showMessageDialog(panel,
							"There are currently no anime's to remove");
				} else {
					int returnValue = JOptionPane.showConfirmDialog(panel,
							"Are you sure you want to remove this anime?");
					if (returnValue == JOptionPane.YES_OPTION) {
						list.remove(list.get(index));
						FileOutputStream fos = null;
						ObjectOutputStream out = null;
						try {
							fos = new FileOutputStream(FILENAME);
							out = new ObjectOutputStream(fos);
							out.writeObject(list);
							out.close();
						} catch (IOException ex) {
							System.out.print("File not found");
						}
						update();
					}
				}
			}
		}

		@Override
		public void valueChanged(ListSelectionEvent event) {
			if (!event.getValueIsAdjusting()) {
				this.index = jList.getSelectedIndex();
			}

		}
	}
}
